/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ControlLager;
import ModelLager.*;
import DBLager.*;
import java.util.ArrayList;
import java.sql.*;
/**
 *
 * @author Ronni
 */
public class SalesOrderPCTR implements IFDbSalesOrderP{

    private IFDbSalesOrderP dbSalesOrder;
    private IFDbInVoiceP dbInvoice;
    private DbPartOrderP dbPartOrder;
    private DbSalesOrderP salesOrder;
    
    public SalesOrderPCTR() 
    {
        dbSalesOrder = new DbSalesOrderP();
        dbInvoice = new DbInVoiceP();
        dbPartOrder = new DbPartOrderP();
        salesOrder = new DbSalesOrderP();
        
    }
    
//public int generateOrderID()
//{
//int ordreID = 0;
//    for(SalesOrderP salesorders : dbSalesOrder.findAllSalesOrders())
//    {
//        if(salesorders.getOrderID() > ordreID)
//        {
//            ordreID = salesorders.getOrderID();
//            ordreID++;
//        }
//    }
//    return ordreID;
//}
    
 public int createOrder(Date date, double totalPrice, int deliveryStatus, Date deliveryDate, double VAT, double totalPriceNoVAT, CustomerP customerID)

    {
        
        int status = 1;
        try{
            System.out.println(salesOrder.getNextOrderID());
        int newOrderID = salesOrder.getNextOrderID();
        //InVoiceP newInvoice = new InVoiceP(newOrderID, "Today", 10000000);
        SalesOrderP sOrd = new SalesOrderP(date, totalPrice, deliveryStatus, deliveryDate, VAT, totalPriceNoVAT, customerID, newOrderID, null);
        dbSalesOrder.insertSalesOrder(sOrd);
        //dbInvoice.insertInvoice(newInvoice);
        //System.out.println("OrderID: " + sOrd.getOrderID() + " CustomerName: " + sOrd.getCustomer().getName());
        }catch(Exception e){
            status = -1;
            System.out.println("createOrder Error: " + e.getMessage());
        }
        return status;
            
    }  
 
     public ArrayList<PartOrderP> getAllPartOrders()
    {
        return dbPartOrder.getAllPartOrders();
    }
 
 public void createPartOrder(int amountItem, SalesOrderP salesOrder, ProductP product)
 {
     PartOrderP pOrder = new PartOrderP(amountItem, salesOrder, product);
     try{
     int rc = dbPartOrder.insertPartOrderP(pOrder);
     dbSalesOrder.findSalesOrder(salesOrder.getOrderID()).addPartOrder(pOrder);
     
     }catch(Exception e)
     {
         System.out.println(e.getMessage());
     }
     
 }
 
 public void addPartOrder(int orderID)
 {
  dbSalesOrder.findSalesOrder(orderID).addPartOrder(null);   
     
 }
 public void createInVoice(int inVoiceNo, String paymentDate, int amount)
 {
     InVoiceP newInvoice = new InVoiceP(inVoiceNo,paymentDate,amount);
 }
    
    @Override
    public SalesOrderP findSalesOrder(int orderID)
    {
        return dbSalesOrder.findSalesOrder(orderID);
    }
    @Override
    public ArrayList<SalesOrderP> findAllSalesOrders()
    {
        return dbSalesOrder.findAllSalesOrders();
    }
    @Override
    public int insertSalesOrder(SalesOrderP so)
    {
        return dbSalesOrder.insertSalesOrder(so);
    }
    @Override
    public int updateSalesOrder(SalesOrderP so)
    {
        return dbSalesOrder.updateSalesOrder(so);
    }
    @Override
    public int deleteSalesOrder(SalesOrderP so)
    {
        return dbSalesOrder.deleteSalesOrder(so);
    }
    
}
